package com.ruoyi.income.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MoneyUtils;
import com.ruoyi.contarct.domain.HouseContarct;
import com.ruoyi.contarct.enums.IncomeFeeType;
import com.ruoyi.contarct.enums.IncomeFlowType;
import com.ruoyi.contarct.mapper.HouseContarctMapper;
import com.ruoyi.contarctbill.domain.HouseContarctBill;
import com.ruoyi.contarctbill.dto.PayIncomeDTO;
import com.ruoyi.contarctbill.mapper.HouseContarctBillMapper;
import com.ruoyi.income.domain.SumContarctIncome;
import com.ruoyi.incomerecord.domain.HouseContarctIncomeRecord;
import com.ruoyi.incomerecord.mapper.HouseContarctIncomeRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.income.mapper.HouseContarctIncomeMapper;
import com.ruoyi.income.domain.HouseContarctIncome;
import com.ruoyi.income.service.IHouseContarctIncomeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 计划详情Service业务层处理
 * 
 * @author hct
 * @date 2022-02-16
 */
@Service
public class HouseContarctIncomeServiceImpl implements IHouseContarctIncomeService 
{
    @Autowired
    private HouseContarctIncomeMapper houseContarctIncomeMapper;

    @Autowired
    private HouseContarctIncomeRecordMapper houseContarctIncomeRecordMapper;

    @Autowired
    private HouseContarctBillMapper houseContarctBillMapper;

    @Autowired
    private HouseContarctMapper houseContarctMapper;

    /**
     * 查询计划详情
     * 
     * @param id 计划详情主键
     * @return 计划详情
     */
    @Override
    public HouseContarctIncome selectHouseContarctIncomeById(Long id)
    {
        return houseContarctIncomeMapper.selectHouseContarctIncomeById(id);
    }

    /**
     * 查询计划详情列表
     * 
     * @param houseContarctIncome 计划详情
     * @return 计划详情
     */
    @Override
    public List<HouseContarctIncome> selectHouseContarctIncomeList(HouseContarctIncome houseContarctIncome)
    {
        return houseContarctIncomeMapper.selectHouseContarctIncomeList(houseContarctIncome);
    }

    /**
     * 新增计划详情
     * 
     * @param houseContarctIncome 计划详情
     * @return 结果
     */
    @Override
    public int insertHouseContarctIncome(HouseContarctIncome houseContarctIncome)
    {
        return houseContarctIncomeMapper.insertHouseContarctIncome(houseContarctIncome);
    }

    /**
     * 修改计划详情
     * 
     * @param houseContarctIncome 计划详情
     * @return 结果
     */
    @Override
    public int updateHouseContarctIncome(HouseContarctIncome houseContarctIncome)
    {
        return houseContarctIncomeMapper.updateHouseContarctIncome(houseContarctIncome);
    }

    /**
     * 批量删除计划详情
     * 
     * @param ids 需要删除的计划详情主键
     * @return 结果
     */
    @Override
    public int deleteHouseContarctIncomeByIds(Long[] ids)
    {
        return houseContarctIncomeMapper.deleteHouseContarctIncomeByIds(ids);
    }

    /**
     * 删除计划详情信息
     * 
     * @param id 计划详情主键
     * @return 结果
     */
    @Override
    public int deleteHouseContarctIncomeById(Long id)
    {
        return houseContarctIncomeMapper.deleteHouseContarctIncomeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object payCurrMonthBill(List<PayIncomeDTO> incomeList, String userName, Long userId) throws Exception {
        Date now = new Date();
        Long billId=0L;
        BigDecimal allysfee=new BigDecimal(0);
        for (PayIncomeDTO income: incomeList
             ){
            Long incomeId = income.getIncomeId();
            Double price = income.getPrice();
            HouseContarctIncome houseContarctIncome = houseContarctIncomeMapper.selectHouseContarctIncomeById(incomeId);
            BigDecimal fee = houseContarctIncome.getFee();
            BigDecimal ysfee = houseContarctIncome.getYsfee();
            BigDecimal pri = new BigDecimal(price);
            BigDecimal subfee = fee.subtract(pri);
            BigDecimal addysfee = ysfee.add(pri);
            houseContarctIncome.setYsfee(addysfee);
            houseContarctIncome.setUpdateTime(now);
            houseContarctIncome.setUpdateBy(userName);
            houseContarctIncomeMapper.updateHouseContarctIncome(houseContarctIncome);
            allysfee = allysfee.add(addysfee);
            billId=houseContarctIncome.getBillId();
            HouseContarctIncomeRecord record=new HouseContarctIncomeRecord();
            record.setCreateTime(now);
            record.setFee(pri);
            record.setFeetime(now);
            record.setUpdateTime(now);
            record.setContarctid(houseContarctIncome.getContarctId());
            record.setIncomeId(houseContarctIncome.getId());
            record.setContarctPlandate(DateUtils.dateToString("yyyy-MM-dd hh:mm:ss",now)+"收款"+ subfee+"元");
            record.setPrefee(ysfee);
            record.setLastFee(addysfee.doubleValue()+"");
            record.setFlowtype(houseContarctIncome.getFlowtype());
            record.setFeetype(houseContarctIncome.getFeetype());
            record.setFeetypeid(houseContarctIncome.getFeetypeid());
            houseContarctIncomeRecordMapper.insertHouseContarctIncomeRecord(record);
        }
        HouseContarctBill houseContarctBill= houseContarctBillMapper.selectHouseContarctBillById(billId);
        HouseContarctBill houseContarctBillNew=new HouseContarctBill();
        houseContarctBillNew.setId(billId);
        allysfee = allysfee.divide(new BigDecimal(1D), 2, BigDecimal.ROUND_HALF_UP);
        houseContarctBillNew.setYsfee(allysfee);
        houseContarctBill.setFee(houseContarctBill.getFee().divide(new BigDecimal(1D), 2, BigDecimal.ROUND_HALF_UP));

        if(houseContarctBill.getFee().equals(allysfee) || allysfee.compareTo(houseContarctBill.getFee()) == 1){
            houseContarctBillNew.setIsover(1);
        }
        houseContarctBillMapper.updateHouseContarctBill(houseContarctBillNew);
        return "ok";
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object makeAllfeeItem(Long contractId, Long feeSetId,Float fee,String userName) throws Exception {
        Date now =new Date();
        //合同查账单
        HouseContarctBill bill = new HouseContarctBill();
        bill.setContarctId(contractId);
        List<HouseContarctBill> houseContarctBills = houseContarctBillMapper.selectHouseContarctBillList(bill);
        for (HouseContarctBill hcBill: houseContarctBills
             ){
            BigDecimal feeb = new BigDecimal(fee.doubleValue());
            HouseContarctIncome hcIncome =new HouseContarctIncome();
            hcIncome.setFee(feeb);
            hcIncome.setContarctId(contractId);
            hcIncome.setBillId(hcBill.getId());
            hcIncome.setYsfee(new BigDecimal(0D));
            hcIncome.setFlowtype(Long.parseLong(IncomeFlowType.IN.getCode()+""));
            hcIncome.setFeetype(IncomeFeeType.CHANGE.getCode());
            hcIncome.setFeetypeid(feeSetId);
            houseContarctIncomeMapper.insertHouseContarctIncome(hcIncome);
        }
        HouseContarct houseContarct = houseContarctMapper.selectHouseContarctById(contractId);
        return houseContarct;
    }


    /**
     * 记录首页数量
     * @param houseContarctIncome
     * @return
     */
    public SumContarctIncome sumIncome(HouseContarctIncome houseContarctIncome){
        return houseContarctIncomeMapper.sumIncome(houseContarctIncome);
    }
}
